package handler

import (
	"github.com/gin-gonic/gin"
	"onvif_server/api"
	"onvif_server/api/setting"
	"onvif_server/internal/errcode"
	"onvif_server/internal/service"
)

type SettingHandler struct {
	*Handler
	settingService service.SettingService
}

func NewSettingHandler(handler *Handler, settingService service.SettingService) *SettingHandler {
	return &SettingHandler{
		Handler:        handler,
		settingService: settingService,
	}
}

// network
func (h *SettingHandler) GetNetwork(ctx *gin.Context) {
	res, err := h.settingService.GetNetwork(ctx)
	api.Response(ctx, err, res)
}

func (h *SettingHandler) SetNetwork(ctx *gin.Context) {
	req := setting.SetNetworkReq{}
	if err := ctx.ShouldBindJSON(&req); err != nil {
		api.Fail(ctx, errcode.ErrInvalidParams.Wrap(err))
		return
	}
	api.Response(ctx, h.settingService.SetNetwork(ctx, &req), nil)
}
func (h *SettingHandler) GetStorage(ctx *gin.Context) {
	res, err := h.settingService.GetStorage(ctx)
	api.Response(ctx, err, res)
}

func (h *SettingHandler) SetStorage(ctx *gin.Context) {
	req := setting.StoreReq{}
	if err := ctx.ShouldBindJSON(&req); err != nil {
		api.Fail(ctx, errcode.ErrInvalidParams.Wrap(err))
		return
	}
	api.Response(ctx, h.settingService.SetStorage(ctx, &req), nil)
}
func (h *SettingHandler) GetBox(ctx *gin.Context) {
	res, err := h.settingService.GetBox(ctx)
	api.Response(ctx, err, res)
}

func (h *SettingHandler) SetBox(ctx *gin.Context) {
	req := setting.BoxInfo{}
	if err := ctx.ShouldBindJSON(&req); err != nil {
		api.Fail(ctx, errcode.ErrInvalidParams.Wrap(err))
		return
	}
	api.Response(ctx, h.settingService.SetBox(ctx, &req), nil)
}

func (h *SettingHandler) StorageDrivers(ctx *gin.Context) {
	api.Success(ctx, h.settingService.StorageDrivers(ctx))
}
